#ifndef _NETKIT_CONNECTION_MANAGER_H_
#define _NETKIT_CONNECTION_MANAGER_H_

#include "connection.h"
#include "tcp_socket.h"
#include "processor.h"
#include <list>
#include <stdint.h>

namespace NetKit
{

enum EVENT_TYPE
{
	NETWORK_EVENT = 0,
	THREAD_TASK_EVENT = 1
};

struct ConnectionItem
{
	EVENT_TYPE event_type;
	Connection* conn;
	ConnectionItem()
	{
		event_type = NETWORK_EVENT;
		conn = NULL;
	}
};

class ConnectionManager 
{
public:
	ConnectionManager();
    virtual ~ConnectionManager();

	void AddConnection(Processor* pProcessor, TcpSocket* pSock);
	void AddConnection(int fd, Connection* pConn, EVENT_TYPE event_type = THREAD_TASK_EVENT);
	
	void RemoveConnection(Connection* pConn);
	void RemoveConnection(int socket_pair_fd, Connection* pConn);
	
	ConnectionItem* GetConnectionItem(int fd);
	
private:
	void Grow(int maxFd);
	
private:
	//Connection** m_ppConns;
	std::list<Connection*> m_lstIdleConn;
	ConnectionItem* m_pConnItem;
	
	uint32_t m_iMaxFd;
	uint32_t m_iUsed;
};

}

#endif // _NETKIT_CONNECTION_MANAGER

